package view.table;

import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;

import view.DigitLimited;
import controller.IExchangerController;

public class ExchangerTableCellEditor extends DefaultCellEditor {
	private IExchangerController control;
	private JTextField editorField = new JTextField();

	public ExchangerTableCellEditor(IExchangerController control,
			JTextField textField) {
		super(textField);
		this.control = control;
		textField.setOpaque(true);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public Component getTableCellEditorComponent(JTable table,
			Object value, boolean isSelected, int row,int column) {
		// TODO Auto-generated method stub
		if (value instanceof String) {
			editorField = (JTextField) super.getTableCellEditorComponent(table,
					value, isSelected, row, column);
			editorField.setDocument(new DigitLimited(editorField));
			editorField.setText((String) value);
			editorField.setName((String) table.getValueAt(row, 2));
		}
		return editorField;
	
	}

	// This method is called when editing is completed.
	// It must return the new value to be stored in the cell.
	public Object getCellEditorValue() {
		control.changeRate(editorField.getName(),
				Double.parseDouble(editorField.getText()));
		return editorField.getText();
	}

}
